import argparse
import spacy

nlp = spacy.load("en_core_web_sm")

def split_text(text):
    doc = nlp(text)
    tokenS = []
    for sent in doc.sents:
        for token in sent:
            if token.is_space:
                if len(tokenS) > 0:
                    yield " ".join(tokenS)
                    tokenS = []
            else:
                tokenS.append(token.text)
        if len(tokenS) > 0:
            yield " ".join(tokenS)
            tokenS = []
    if len(tokenS) > 0:
        yield " ".join(tokenS)

parser = argparse.ArgumentParser()
parser.add_argument("file_path", help="path to input file")
args = parser.parse_args()

with open(args.file_path, "r") as f:
    text = f.read()

splited_text = split_text(text)

for clause in splited_text:
    if len(clause) <= 40:
        print(clause)
    else:
        i = 0
        while i < len(clause):
            if i + 40 >= len(clause):
                print(clause[i:])
                break
            elif clause[i+40].isspace():
                print(clause[i:i+40])
                i += 40
            else:
                j = i + 40
                while j > i and not clause[j].isspace():
                    j -= 1
                if j == i:
                    print(clause[i:i+40])
                    i += 40
                else:
                    print(clause[i:j])
                    i = j + 1
